<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<HTML>
<HEAD>
<title></title>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
</HEAD>

<html>
  <head> 
	   <title>找回密码</title>
  </head>
  <script>
     var userId = 0;
     function isEmail(str) {
		if (str.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
			return true;
		} else {
			return false;
	    }
	  }
		
  	  function next1(){
  	     var userName = $("#findUsername").val();
  	     if(userName.replace(/\s/g , '') == ''){
  	        $("#msg").text("cts帐号不能为空");
  	        return;
  	     }
  	     
  	     if(!isEmail(userName)){
  	        $("#msg").text("请输入正确的cts帐号");
  	        return;
  	     }
  	     
		 var url = "cts/findUserByType.action?type=1&returnUid=0&username="+userName;
		 $.post(url , "" , function(data){
		     if(data >1){  // 成功
		        userId = data;
		        sendMail(userName , data , userName);
		        $("#msg").text("");
		        //$("#first").hide();
		        //$("#seconed").show();
		        
		     }else if(data == 0){ // 失败
		        $("#msg").text("cts帐号不存在!");
		     }else{ // 出错
		        $("#msg").text("出错了，请稍后再试!");
		     }
		 });
  	  }
  	  
  	  function next2(){
  	     var mail = $("#umail").val();
  	     var userName = $("#findUsername").val();
  	     if(mail.replace(/\s/g , '') == '' || !isEmail(mail)){
  	        $("#msg").text("请输入正确的邮箱");
  	        return;
  	     }
  	     
		 sendMail(userName , userId , mail);
  	  }
  	  
  	  function sendMail(userName , data , email){
	   	 var url = 'cts/sendPassword.action?userName='+encodeURI(encodeURI(userName)) +'&userId='+data + "&email="+email;
		    $.post(url , function(data) {		        	
	        	if(data == 1){
	        	   loginMail(email);
	        	   //$("#seconed").hide();
	        	   //$("#third").show();
	        	   
	        	   $("#first").hide();
	        	   $("#third").show();
	        	}else{
	        	   alert("操作失败");
	        	}
		   	}); 
  	  }
  	  
  	  function loginMail( mail ){
  	  
        var fix = mail.split("@")[1].split(".")[0];
        
        var url = '';
        if( fix == '126' ){
        	url = 'http://mail.126.com/';
        	//window.parent.location.href = url;
        }else if( fix == '163' ){
        	url = 'http://mail.163.com/';
        	//window.parent.location.href = url;
        }else if( fix == 'yahoo' ){
        	url = 'http://mail.cn.yahoo.com/';
        	//window.parent.location.href = url;
        }else if( fix == 'sina' ){
           url = 'http://mail.sina.com/';
           window.parent.location.href = url;
        }else if( fix == 'qq' ){
           url = 'https://mail.qq.com/';
           //window.parent.location.href = url;
        }else if( fix == 'hotmail' ){
           url = 'http://hotmail.com/';
           //window.parent.location.href = url;
        }else if (fix == 'sohu'){
           url='http://mail.sohu.com/';
           //window.parent.location.href = url;
        }else{
            alert('<s:text name="m68.getpwdContent12"/>');
        } 
        
        if(url!=''){
          $("#toShareMail").attr("href" , url);
        }       
     }
     
     
     function changepass(){
        var password = $("#password").val();
        var password2 = $("#enter_password").val();
        var userId = $("#userId").val();
        
        var vailPass = passwordMsg(password , password2);
        if(vailPass.isOk == false){
           alert(vailPass.msg);
           return;
        }
        
        var url = "cts/findPassword?userId="+userId+"&password="+password;
        $.post(url , "" , function(data){
             if(data == 1){
                 alert("操作成功");
                 window.location.href = 'index';
             }else if(data == 2){
                 alert("此路径已过期");
             }else{
                 alert("操作失败");
             }
        });
        
     }
     
     function passwordMsg(passTxt , passTxt2){
        var mb = {};
        mb.isOk = true;
        mb.msg = true;
        if(passTxt == null || passTxt == ''){
           mb.msg = '密码不能为空';
           mb.isOk = false;
        }else if(passTxt.length <6){
           mb.msg = '密码最少长度为6位';
           mb.isOk = false;
        }else if(passTxt.length >25){
           mb.msg = '密码最长长度为25位';
           mb.isOk = false;
        }else{
	       if(typeof(passTxt2)!='undefined'){
	           if(passTxt!=passTxt2){
	             mb.msg = '二次输入的密码不一致';
	             mb.isOk = false;
	           }
	       }
        }
        
        
        return mb;
     }
  </script>
  <body>
       	<div id="regContent" style="height:200px;" align="center">
       		    <s:if test="#request.setup == 2">
       		        <s:if test="#request.isOnTime == 1">
	       		       <input type="hidden" id="userId" name="n_userId" value="<s:property value="#request.userId"/>"/>
	       		       <div id="findPassword">
			  				<table cellspacing="0" border="0" style="border:1px solid #ececec;background-color:#a9a9a9;font-size:12px;width:300px;height:160px;"> 
			  				    <tr>
			  				      <td colspan="2" style="border-bottom:1px solid #ececec;height:30px;font-size:14px;padding-left:5px;font-weight: bold;">找回密码</td>
			  				    </tr>
								<tr> 
								    <td align="right">cts帐号：</td>
									<td align="left">
										<s:property value="#request.userName"/>
									</td>
								</tr>
								<tr>
								    <td align="right">密码：</td>
									<td align="left">
										<input type="password" id="password" name="n_password"/>
									</td>
								</tr>
								<tr>
								    <td align="right">确认密码：</td>
									<td align="left">
										<input type="password"" id="enter_password" name="n_enter_password"/>
									</td>
								</tr>
								<tr>
								   <td colspan="2" align="center">
								      <input type="button" id="enterChangePass" name="n_enterChangePass" value="确认" onclick="changepass()"/>
								   </td>
								</tr>
							</table>
		 				</div>
       		        </s:if>
       		        <s:else>
       		           <div id="noWorkPath" style="width:100%;height:100%;color:red;">此路径已过期</div>
       		        </s:else>
       		    </s:if>
       		    <s:else>
	       		    <div id="first">
		  				<table cellspacing="0" border="0" style="border:0;"> 
							<tr>
								<td><span id="msg" style="color:orange;font-size:12px;"></span></td>
							</tr>
							<tr height="40px">
								<td align="left">
									cts帐号：
									<input style="height: 20px;line-height: 20px;vertical-align: middle;border: 1px solid #ddd" type="text" name="username" id="findUsername" class="inputfield0"/>
								</td>
							</tr>
						
							<tr height="60px">
								<td align="center">
									<input class="bg_73_25" type="button" value="下一步" onclick="next1()"/>
								</td>
							</tr>
						</table>
	 				</div>
	 				<div id="seconed" style="display:none;">
	 				   <table cellspacing="0" border="0" style="border:0;width:100%;height:100%;">
	 				      <tr>
	 				         <td>
	 				             邮箱：<input style="height: 20px;line-height: 20px;vertical-align: middle;border: 1px solid #ddd" type="text" name="umail" id="umail" class="inputfield0"/>
	 				         </td>
	 				      </tr>
	 				      <tr height="60px">
								<td align="center">
									<input class="bg_73_25" type="button" value="发送邮箱" onclick="next2()"/>
								</td>
						 </tr>
	 				   </table>
	 				</div>
	 				<div id="third" style="display:none;">
	 				   <table cellspacing="0" border="0" style="border:0;width:100%;height:100%;">
	 				      <tr>
	 				         <td style="text-align:center;color:red;">
	 				            <a href="#" id="toShareMail" target="_blank"><p>邮件发送成功，点击到邮箱</p></a>
	 				         </td>
	 				      </tr>
	 				   </table>
	 				</div>
       		    </s:else>
 				
       	</div>
  </body>
</html>
